//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<XEditorMain.h>
///	@path	~/src/editor/wm/
///	@date	2008/10/25
///	@desc	.

#pragma once

#pragma message("Compiling XEditorMain.h ...")

#include "editor/editor.h"
#include "editor/wm/XFormView.h"
#include "editor/wm/XEditorWorkspace.h"

namespace xeres {

/*
	Editor's layout.

	-------------------------------------------------------
	Shelf region
	-------------------------------------------------------
    Tools  |                                 |
	region |     Workspace view              | Panel region
           |                                 |
	-------------------------------------------------------
	Operation region
	-------------------------------------------------------
	Shell region
	-------------------------------------------------------
	Cue region
	-------------------------------------------------------
*/

	/*! 
		\class	XEditorMain
		\brief
	*/
	class XEditorMain : public CWindowImpl< XEditorMain >
	{
	public:

		// layout names
		enum LayoutRegion
		{
			LAYOUT_SHELF	= 0,
			LAYOUT_TOOLS ,
			LAYOUT_WORKSPACE ,
			LAYOUT_PANELS ,
			LAYOUT_OPERATION ,
			LAYOUT_SHELL ,
			LAYOUT_CUE ,
			LAYOUT_last
		};

		DECLARE_WND_CLASS( _T("xeres__XEditorMain") )

		/// \ctor
		XEditorMain( void ) 
			: m_shelfHeight( 0 )
			, m_toolsWidth( 0 )
			, m_panelWidth( 0 )
			, m_operationHeight( 0 )
			, m_shellHeight( 0 )
			, m_cueHeight( 0 )
		{
			m_regions[LAYOUT_SHELF] = &m_shelf;
			m_regions[LAYOUT_TOOLS] = &m_tools;
			m_regions[LAYOUT_WORKSPACE] = &m_workspace;
			m_regions[LAYOUT_PANELS] = &m_panel;
			m_regions[LAYOUT_OPERATION] = &m_operation;
			m_regions[LAYOUT_SHELL] = &m_shell;
			m_regions[LAYOUT_CUE] = &m_cue;

			m_shelfHeight = 64;
			m_toolsWidth = 48;
			m_panelWidth = 200;
			m_operationHeight = 32;
			m_shellHeight = 25;
			m_cueHeight = 22;
		}
		/// \dtor
		~XEditorMain( void ) {
		}

		BOOL PreTranslateMessage(MSG* pMsg)
		{
			UNREFERENCED_PARAMETER( pMsg );
			return FALSE;
		}

		BEGIN_MSG_MAP(XEditorMain)
			MESSAGE_HANDLER(WM_CREATE, OnCreate)
			MESSAGE_HANDLER(WM_PAINT , OnPaint)
			MESSAGE_HANDLER(WM_SIZE , OnSize)
			MESSAGE_HANDLER(WM_ERASEBKGND , OnEraseBkgnd)
		END_MSG_MAP()

		LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {

			// Create layouts
			m_shelf.Create( m_hWnd , rcDefault , L"shelf" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );
			m_tools.Create( m_hWnd , rcDefault , L"tools" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );
			m_workspace.Create( m_hWnd , rcDefault , L"workspace" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );
			m_panel.Create( m_hWnd , rcDefault , L"panel" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );
			m_operation.Create( m_hWnd , rcDefault , L"operation" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );
			m_shell.Create( m_hWnd , rcDefault , L"shell" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );
			m_cue.Create( m_hWnd , rcDefault , L"cue" , WS_VISIBLE|WS_CHILD|WS_CHILDWINDOW|WS_CLIPSIBLINGS );

			return S_OK;
		}
		LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
			CPaintDC dc( m_hWnd );
			return S_OK;
		}
		LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled ) {
			// resize main frame
			Resize();
			bHandled = FALSE;
			return S_OK;
		}
		LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
			return S_OK;
		}

		/// \brief	Resize frame.
		void Resize( void )
		{
			CRect rect;
			GetClientRect( &rect );

			// re-calculate all internal layout region
			for( int i = 0 ; i < LAYOUT_last ; ++i )
				m_rects[i] = rect;

			int width = rect.Width();
			int height = rect.Height();

			int bottom_height = m_cueHeight + m_shellHeight + m_operationHeight;
			int split = height - bottom_height;

			m_rects[ LAYOUT_SHELF ].bottom = m_shelfHeight;
			m_rects[ LAYOUT_TOOLS ].top = m_rects[ LAYOUT_SHELF ].bottom;
			m_rects[ LAYOUT_TOOLS ].bottom = split;
			m_rects[ LAYOUT_OPERATION ].top = split;
			m_rects[ LAYOUT_OPERATION ].bottom = split + m_operationHeight;
			m_rects[ LAYOUT_SHELL ].top = m_rects[ LAYOUT_OPERATION ].bottom;
			m_rects[ LAYOUT_SHELL ].bottom = m_rects[ LAYOUT_SHELL ].top + m_shellHeight;
			m_rects[ LAYOUT_CUE ].top = m_rects[ LAYOUT_SHELL ].bottom;
			m_rects[ LAYOUT_CUE ].bottom = height;

			m_rects[ LAYOUT_PANELS ] = m_rects[ LAYOUT_WORKSPACE ] = m_rects[ LAYOUT_TOOLS ];

			m_rects[ LAYOUT_TOOLS ].right = m_rects[ LAYOUT_WORKSPACE ].left = m_toolsWidth;
			m_rects[ LAYOUT_PANELS ].left = m_rects[ LAYOUT_WORKSPACE ].right = width - m_panelWidth;

			for( int i = 0 ; i < LAYOUT_last ; ++i )
			{
				if( m_regions[i] )
					m_regions[i]->SetWindowPos( NULL , &(m_rects[i]) , 0 );
			}
		}

		/// \brief	Set shelf height.
		void SetShelfHeight( int shelf_height )
		{
			m_shelfHeight = shelf_height;
		}

		/// \brief	Set tools width.
		void SetToolsWidth( int tools_width )
		{
			m_toolsWidth = tools_width;
		}

		/// \brief	Set panel width.
		void SetPanelWidth( int panel_width )
		{
			m_panelWidth = panel_width;
		}

		/// \brief	Set operation height.
		void SetOperationHeight( int operation_height )
		{
			m_operationHeight = operation_height;
		}

		/// \brief	Set shell height.
		void SetShellHeight( int shell_height )
		{
			m_shellHeight = shell_height;
		}

		/// \brief	Set cue height.
		void SetCueHeight( int cue_height )
		{
			m_cueHeight = cue_height;
		}

		/// \brief	Get sub view.
		CWindow * GetView( LayoutRegion region )
		{
			return m_regions[region];
		}

		/// \brief	Get workspace.
		XEditorWorkspace& GetWorkspace( void ) { return m_workspace; }

		/// \brief	Get shelf.
		XFormView& GetShelf( void ) { return m_shelf; }

		/// \brief	Get tools.
		XFormView& GetTools( void ) { return m_tools; }

		/// \brief	Get panel.
		XFormView& GetPanel( void ) { return m_panel; }

		/// \brief	Get operation.
		XFormView& GetOperation( void ) { return m_operation; }

		/// \brief	Get shell.
		XFormView& GetShell( void ) { return m_shell; }

		/// \brief	Get cue.
		XFormView& GetCue( void ) { return m_cue; }

	private:

		//@ data

		// specific layout reserve width & height
		int				m_shelfHeight;
		int				m_toolsWidth;
		int				m_panelWidth;
		int				m_operationHeight;
		int				m_shellHeight;
		int				m_cueHeight;

		CRect			m_rects[LAYOUT_last];
		CWindow *		m_regions[LAYOUT_last];

		// forms
		XFormView			m_shelf;
		XFormView			m_tools;
		XFormView			m_panel;
		XFormView			m_operation;
		XFormView			m_shell;
		XFormView			m_cue;
		XEditorWorkspace	m_workspace;
	};

} // namespace xeres
